﻿using System.Collections.ObjectModel;
using Microsoft.Win32;
using Hyperbolic;
using System.ComponentModel;
using System.Security.AccessControl;
using Hyperbolic.Model;

namespace HyperbolicTest
{
    public class RegistryNode : IBindingNode
    {
        #region fields
        private RegistryKey _key;
        private int _level;
        private ObservableCollection<IBindingNode> _children = new ObservableCollection<IBindingNode>();

        public string MyName {
            get
            {
                return "[" + _key.Name.Substring(_key.Name.LastIndexOf('\\') + 1) + "]";
            }
        }

        #endregion

        #region properties

        public RegistryKey Key
        {
            get { return _key; }
        }

        #endregion

        #region ctor

        public RegistryNode(RegistryKey key, int level)
        {
            _key = key;
            _level = level;


            try
            {
                string[] __subKeyNames = _key.GetSubKeyNames();
                for (int i = 0; i < __subKeyNames.Length; i++)
                {
                    _children.Add(new RegistryNode(_key.OpenSubKey(__subKeyNames[i]), this.Level + 1));
                }
            }
            catch (System.Security.SecurityException)
            {
                //Silent
            }

        }

        #endregion

        #region INode Members

        public ObservableCollection<IBindingNode> Children
        {
            get
            {
                return _children;
            }
        }

        public bool IsLeaf
        {
            get { return _children.Count == 0; }
        }

        public int Level
        {
            get { return _level; }
        }

        public string Name
        {
            get { return _key.Name.Substring(_key.Name.LastIndexOf('\\') + 1); }            
        }

        public string Description
        {
            get
            {
                return _key.Name;
                }
        }

        #endregion
    }
}
